package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.RandomBox;
import com.atguigu.mock.util.RandomCollection;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.tms.mock.bean.BaseComplex;
import com.atguigu.tms.mock.bean.UserAddress;
import com.atguigu.tms.mock.bean.UserInfo;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.mapper.UserAddressMapper;
import com.atguigu.tms.mock.service.BaseComplexService;
import com.atguigu.tms.mock.service.UserAddressService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 地址簿 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-03-20
 */
@Service
public class UserAddressServiceImpl extends AdvServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {



    @Autowired
    BaseComplexService baseComplexService;



    @Override
    protected Long getId(UserAddress userAddress ){
        return userAddress.getUserId();
    }


    public void   genCustomerAddress(List<UserInfo> userInfoList){

        Map<Long, BaseComplex> baseComplexMap = baseComplexService.all(true);



        List<UserAddress> userAddressList =new ArrayList<>(userInfoList.size());
        for (UserInfo userInfo : userInfoList) {

            UserAddress userAddress =new UserAddress();
            BaseComplex baseComplex = baseComplexService.getOneByRandom();

            userAddress.setProvinceId(baseComplex.getProvinceId());
            userAddress.setCityId(baseComplex.getCityId());
            userAddress.setDistrictId(baseComplex.getDistrictId());
            userAddress.setComplexId(baseComplex.getId());
            userAddress.setAddress(baseComplex.getDistrictName()+baseComplex.getComplexName()+ RandomNum.getRandInt(1,3)+"号楼"+RandomNumString.getRandNumString(1,5,3,"",true));
            userAddress.setUserId(userInfo.getId());
            userAddress.setCreateTime(new Date());
            userAddress.setIsDefault(true);
            userAddress.setIsDeleted("0");
            userAddress.setPhone(userInfo.getPhoneNum());
            userAddressList.add(userAddress);
            userInfo.setDefaultAddress(userAddress);
        }
       saveOrUpdateBatch(userAddressList,1000);


    }
}
